<?php
namespace app\clerk\model;
use app\common\model\ShopClerk as ShopClerkModel;
use think\Session;

/**
 * 商家用户模型
 */
class ShopClerk extends ShopClerkModel
{
    /**
     * 商家用户登录
     */
    public function login($data)
    {
        if (!$user = self::useGlobalScope(false)->with(['shop'])->where([
            'mobile' => $data['user_name'],
            'pwd' => hema_hash($data['password']),
			'status' => 20
        ])->find()) {
            $this->error = '登录失败, 用户名或密码错误';
            return false;
        }
        if (empty($user['shop'])) {
            $this->error = '登录失败, 未找到门店信息';
            return false;
        }
        // 保存登录状态
        Session::set('hema_clerk', [
            'user' => [
                'shop_clerk_id' => $user['shop_clerk_id'],
                'mobile' => $user['mobile'],
				'real_name' => $user['real_name'],
            ],
            'shop' => $user['shop']->toArray(),
            'is_login' => true,
        ]);
        return true;
    }

    /**
     * 更新当前店长信息
     */
    public function renew($data)
    {
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
        // 更新店长信息
        if ($this->save([
                'pwd' => hema_hash($data['password']),
            ]) === false) {
            return false;
        }
        return true;
    }

}
